<?php if (empty($this->data)) : // ถ้าไม่มีข้อมูล ?>
<?php echo $this->element('dataNotFound'); ?>
<? else : // ถ้ามีข้อมูล ?>
<table class="data">
    <thead>
        <tr>
            <th class="no">No.</th>
            <th width="130px">Vendor Part no. <?php if ($display_dispatch_no == true) echo '/ <br /> Dispatch no.'; ?></th>
            <th>Stock code  <?php if ($display_dispatch_no == true) echo '/ <br /> Dispatch Date'; ?></th>
            <th>Customer no.</th>
            <th>Customer Name  <?php if ($display_dispatch_no == true) echo '/ <br /> Product Description'; ?></th>
            <th>Site no.</th>
            <th>Site Name</th>
            <th width="50px">Freq.</th>
            <th width="90px">Price</th>
            <th width="50px">Qty.*</th>
            <th width="90px">Amount</th>
        </tr>
    </thead>
    <tbody>
            <?php
            $total_freq = 0;
            $total_qty = 0;
            $total_amount = 0;
            foreach($this->data as $i => $data): ?>
        <tr class="<?php echo ((($i + 1) % 2 == 0)? 'even-cell' : 'odd-cell'); ?>">
            <td class="center"><?php echo $i + 1; ?></td>
            <td class="<?php echo $display_dispatch_no == true? 'bold': ''; ?>">
                        <?php echo $html->link($data['Product']['supplier_part_no'],
                        '/supplier_consignment_lists/view/' . $data['Product']['id'] . '/' . strtotime('now'),
                        array('target' => '_blank')); ?>
            </td>
            <td class="<?php echo $display_dispatch_no == true? 'bold': ''; ?>"><?php echo $data['CustProductSite']['stock_code']; ?></td>
            <td class="<?php echo $display_dispatch_no == true? 'bold': ''; ?>"><?php echo $data['Customer']['cust_no']; ?></td>
            <td class="<?php echo $display_dispatch_no == true? 'bold': ''; ?>"><?php echo $data['Customer']['name']; ?></td>
            <td class="<?php echo $display_dispatch_no == true? 'bold': ''; ?>"><?php echo $data['Site']['site_no']; ?></td>
            <td class="<?php echo $display_dispatch_no == true? 'bold': ''; ?>"><?php echo $data['Site']['name']; ?></td>
            <td class="text-right <?php echo $display_dispatch_no == true? 'bold': ''; ?>"><?php echo $data[0]['frequency']; ?></td>
            <td class="text-right <?php echo $display_dispatch_no == true? 'bold': ''; ?>"><?php echo number_format($data[0]['price'], 2); ?></td>
            <td class="text-right <?php echo $display_dispatch_no == true? 'bold': ''; ?>"><?php echo $data[0]['stock_in_qty']; ?></td>
            <td class="text-right <?php echo $display_dispatch_no == true? 'bold': ''; ?>"><?php echo number_format($data[0]['price'] * $data[0]['stock_in_qty'], 2); ?></td>
            <?php
            $total_freq += $data[0]['frequency'];
            $total_qty += $data[0]['stock_in_qty'];
            $total_amount += $data[0]['price'] * $data[0]['stock_in_qty'];
            ?>
        </tr>
                <?php if ($display_dispatch_no == true) :
                    $stos = $this->requestAction('supplier_reports/get_dispatch_no/' . $data['Product']['id'] . '/' . $data['CustProductSite']['id']);
                    // ตัวแปรสำหรับลำดับเลข dispatch
                    $k = 1;
                    foreach($stos as $sto) :
                        ?>
        <tr class="<?php echo ((($i + 1) % 2 == 0)? 'even-cell' : 'odd-cell'); ?>">
            <td>&nbsp;</td>
                            <?php if ($display_dispatch_no == true) : ?>
            <td style="padding-right: 30px">
                <span><?php echo $k; ?>.</span>&nbsp;
                <span>
                                        <?php echo $html->link($sto['SDH']['dispatch_no'],
                                        '/dispatches/view/' . $sto['SDH']['id'] . '/' . strtotime('now'),
                                        array('target' => '_blank')); ?>
                </span>
            </td>
            <td>&nbsp;<?php echo $sto[0]['dispatch_date']; ?></td>
            <td>&nbsp;</td>
            <td><?php echo $k == 1? $data[0]['text']: ''; ?></td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td class="text-right"><?php echo $sto['SD']['stock_in_qty']; ?></td>
            <td class="text-right"><?php echo number_format($sto['SD']['stock_in_qty'] * $data[0]['price'], 2); ?></td>
                            <?php endif; ?>
        </tr>
                        <?php ++$k;
                    endforeach; ?>
                <?php endif; ?>
            <?php endforeach; ?>
    </tbody>
    <tfoot>
        <tr>
            <td colspan="7" class="bold text-right">Grand Total</td>
            <td class="text-right bold"><?php echo number_format($total_freq); ?></td>
            <td class="text-right">-</td>
            <td class="text-right bold"><?php echo number_format($total_qty); ?></td>
            <td class="text-right bold"><?php echo number_format($total_amount, 2); ?></td>
        </tr>
    </tfoot>
</table>
<span class="caption">* Only Products that have been stocked in by Customer are counted.</span>
<? endif; ?>
<?php //debug($this->data); ?>